﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using PlanningGIS.DataCheck.model;
using System.Collections.Generic;

using PlanningGIS.Framework;

using PlanningGIS.Win.Controls;
using PlanningGIS.Engine.Controls;
using PlanningGIS.ArcGIS;
using PlanningGIS.Framework.Rule;

namespace PlanningGIS.DataCheck.form
{
   
    public partial class FrmAttrLogicCheck : Office2007Form
    {
        private IMapControl3 pMapControl;
        private IFeatureLayer pFeatLyr;
        private string fieldName;
        private List<object> values;
        private IGeometry extent;
        private IMap mMap;
        private readonly bool addOrModify;
        private AttrCheckRule attrCheckRule;
        private IApplication m_Application;

        private bool checkByRule = false;

        //public FrmAttrLogicCheck(bool AddOrModify, IApplication application,
        //                           AttrCheckRule checkRule)
        //{
        //    InitializeComponent();

        //    checkByRule = true;
        //    addOrModify = AddOrModify;
        //    attrCheckRule = checkRule;

        //    if ((application as IFramework).IsMapCtrlactive)
        //    {

        //        pMapControl = (application as IFramework).MapControl;

        //        LayersComboBox.FillFeatLyrName2cbo(cboFeatLyr, pMapControl.Map, esriGeometryType.esriGeometryAny,
        //                       esriFeatureType.esriFTSimple, false, true);

        //        if (cboFeatLyr.Items.Count > 0) cboFeatLyr.SelectedIndex = 0;

        //        cboFeatLyr.Focus();
        //    }

        //    cboExtent.SelectedIndex = 0;

        //    if (addOrModify)
        //    {
        //        Text = "新增检查项";
        //        txtCheckName.Text = "新增检查";
        //        txtCheckName.Focus();
        //    }
        //    else
        //    {
        //        Text = "修改检查项";
        //        if (attrCheckRule == null) return;

        //        txtCheckName.Text = attrCheckRule.CheckName;
        //        cboFeatLyr.Text = attrCheckRule.FeatLyrName;

        //        try
        //        {
        //            cboField.Text = attrCheckRule.FieldName;
        //        }
        //        catch
        //        {
        //            cboField.SelectedIndex = 0;
        //        }
        //        cboExtent.SelectedIndex = Convert.ToInt16(attrCheckRule.SpatialFilter.EnumSpatialFilter);

        //        string tag = attrCheckRule.Tag;
        //        string[] strs = tag.Split(',');
        //        List<string> values = new List<string>();
        //        for (int j = 0; j < strs.GetLength(0); j++)
        //        {
        //            //values[j] = strs[j];
        //            values.Add(strs[j]);
        //        }

        //        dgvXValue.Rows.Clear();
        //        dgvXValue.Rows.Add(values.Count);
        //        int i = 0;
        //        foreach (string value in values)
        //        {
        //            dgvXValue.Rows[i].Cells[0].Value = value;
        //            i++;                    
        //        }
        //    }
        //}

        public FrmAttrLogicCheck(bool AddOrModify, IApplication application,
                           AttrCheckRule checkRule)
        {
            InitializeComponent();

            checkByRule = true;
            addOrModify = AddOrModify;
            attrCheckRule = checkRule;            
            m_Application = application;
            mMap = (m_Application as IFramework).MapControl.Map;
            
            cboFeatLyr.FeatureType = zjgisFeatureType.Vector;
            cboFeatLyr.ShapeType = zjgisShapeType.Line | zjgisShapeType.Point | zjgisShapeType.Polygon | zjgisShapeType.Annotate;
            cboFeatLyr.IsOnlySelected = true;
            cboFeatLyr.OnCreate((m_Application as IFramework).Object);
            

            cboFeatLyr.Focus();
        

            cboExtent.SelectedIndex = 0;

            if (addOrModify)
            {
                Text = "新增检查项";
                txtCheckName.Text = "新增检查";
                txtCheckName.Focus();
            }
            else
            {
                Text = "修改检查项";
                if (attrCheckRule == null) return;

                txtCheckName.Text = attrCheckRule.CheckName;
                cboFeatLyr.SetFeatureClassName(attrCheckRule.FeatLyrName);

                try
                {
                    cboField.Text = attrCheckRule.FieldName;
                }
                catch
                {
                    cboField.SelectedIndex = 0;
                }
                cboExtent.SelectedIndex = Convert.ToInt16(attrCheckRule.SpatialFilter.EnumSpatialFilter);

                string tag = attrCheckRule.Tag;
                string[] strs = tag.Split(',');
                List<string> values = new List<string>();
                for (int j = 0; j < strs.GetLength(0); j++)
                {
                    //values[j] = strs[j];
                    values.Add(strs[j]);
                }

                dgvXValue.Rows.Clear();
                dgvXValue.Rows.Add(values.Count);
                int i = 0;
                foreach (string value in values)
                {
                    dgvXValue.Rows[i].Cells[0].Value = value;
                    i++;
                }
            }
        }
        public FrmAttrLogicCheck(IMapControl3 value)
        {
            InitializeComponent();

            cboExtent.SelectedIndex = 0;

            pMapControl = value;
            mMap = pMapControl.Map;

            cboFeatLyr.FeatureType = zjgisFeatureType.Vector;
            cboFeatLyr.ShapeType = zjgisShapeType.Line | zjgisShapeType.Point | zjgisShapeType.Polygon | zjgisShapeType.Annotate;
            cboFeatLyr.IsOnlySelected = true;
            cboFeatLyr.OnCreate(value);
            
            cboFeatLyr.Focus();
        }

        public IFeatureLayer FeatureLayer
        {
            get { return pFeatLyr; }
        }

        public string FieldName
        {
            get { return fieldName; }
        }

        public List<object> Values
        {
            get { return values; }
        }

        public IGeometry Extent
        {
            get { return extent; }
        }

        private void buttonXExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cboFeatLyr_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cboField.Items.Clear();
                cboField.Items.Clear();

                if (cboFeatLyr.Text == "") return;
                IFeatureLayer pFeatLyr = MapHelper.GetFirstLayer<IFeatureLayer>(mMap, cboFeatLyr.Text);

                if (pFeatLyr == null) return;

                IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

                for (int i = 0; i < pFeatCls.Fields.FieldCount; i++)
                {
                    IField pField = pFeatCls.Fields.get_Field(i);
                    if (pField.Type == esriFieldType.esriFieldTypeString) cboField.Items.Add(pField.Name);
                    if ((int)pField.Type < 4) cboField.Items.Add(pField.Name);
                }
                if (cboField.Items.Count > 0) cboField.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(ex.Message);
            }
        }

        private void buttonXOK_Click(object sender, EventArgs e)
        {
            if (addOrModify)
            {
                attrCheckRule = new AttrCheckRule();
                attrCheckRule.CheckType = "AttrLogicCheck";
                attrCheckRule.NeedCheck = true;
            }

            if (checkByRule)
            {
                if (txtCheckName.Text == "")
                {
                    MsgHelper.ShowInfomationMsgbox("请输入检查名！");
                    return;
                }
                else
                {
                    attrCheckRule.CheckName = txtCheckName.Text;
                }
            }

            if (cboFeatLyr.Text == "")
            {
                MsgHelper.ShowWarningMsgbox("请选择需要检查的图层！");
                return;
            }
            else
            {
                attrCheckRule.FeatLyrName = cboFeatLyr.Text;
            }


            //pFeatLyr = MapHelper.GetFirstLayer<IFeatureLayer>(mMap, cboFeatLyr.Text);

            //if (pFeatLyr == null)
            //{
            //    MsgHelper.ShowWarningMsgbox("["+cboFeatLyr.Text+"]图层未找到！");
            //    return;
            //}

            if (cboField.Text == "")
            {
                MsgHelper.ShowWarningMsgbox("请选择需要检查的字段！");
                return;
            }
            else
            {
                attrCheckRule.FieldName = cboField.Text;
            }

            fieldName = cboField.Text;

            if (dgvXValue.Rows.Count == 0)
            {
                MsgHelper.ShowWarningMsgbox("请设置字段值！");
                return;
            }

            values = new List<object>();

            string tag = "";

            int i = 0;
            foreach (DataGridViewRow dgvr in dgvXValue.Rows)
            {
                i++;
                if (i == dgvXValue.Rows.Count) break;

                object obj = dgvr.Cells[0].Value == null ? "" : dgvr.Cells[0].Value;
                if (obj.ToString().ToUpper() == "NULL")
                {
                    if (!values.Contains(null))
                        values.Add(null);
                    if (!values.Contains(DBNull.Value))
                        values.Add(DBNull.Value);

                    tag = tag + "null,";
                }
                else
                {
                    if (!values.Contains(obj))
                    {
                        values.Add(obj);
                        tag = tag + obj.ToString() + ",";
                    }
                }
            }

            if (tag != "")
                tag = tag.Substring(0, tag.Length - 1);
            else
            {
                MsgHelper.ShowWarningMsgbox("请正确设置字段值！");
                return;
            }

            attrCheckRule.Tag = tag;

            //if (cboExtent.SelectedIndex == 1)
            //{
            //    IActiveView pActiveView = (IActiveView)pMapControl.Map;
            //    if (pActiveView != null) extent = pActiveView.Extent;
            //}
            //else
            //    extent = null;

            SpatialFilterDC spatialFilter = new SpatialFilterDC();
            attrCheckRule.SpatialFilter = spatialFilter;
            spatialFilter.EnumSpatialFilter = cboExtent.SelectedIndex;

            if (checkByRule && addOrModify)
            {
                DataCheckRule currentDataCheckRule = DataCheckUtility.GetCurrentDataCheckRule();
                currentDataCheckRule.AttrCheckRules.Add(attrCheckRule);
            }

            this.DialogResult = DialogResult.OK;
        }

        private void FrmAttrLogicCheck_Load(object sender, EventArgs e)
        {

        }
    }
}
